# -*- coding:utf-8 -*-
# @Author:🎈RedBalloon
# @Time:2022/10/2-14:44
# @File:14-URLError的使用.py
from urllib.request import Request, urlopen
from fake_useragent import UserAgent
from urllib.error import URLError

url = "https://www.baidu.com/22"
headers = {
    "User-Agent": UserAgent().random,
}
try:
    req = Request(url, headers=headers)
    rep = urlopen(req).read()
    print(rep.decode())
except URLError as e:
    print(e)  # HTTP Error 404: Not Found
    # print(e.code)  # 404
# 第一次运行保错，看到ssl且报红行数不多，可以再次运行一次就没有了
